import { faker } from "@faker-js/faker";
import { BlogType } from "@/types/blog";

function createBlogDetail(id: number): BlogType & { content: string } {
  return {
    id,
    title: faker.lorem.sentence({ min: 3, max: 8 }),
    createdAt: faker.date.past().toISOString(),
    tags: faker.lorem.words({ min: 2, max: 5 }).split(" "),
    content: faker.lorem.paragraphs({ min: 2, max: 5 }),
  };
}

export async function GET(request: Request) {
  const { searchParams } = new URL(request.url);
  const id = Number(searchParams.get("id"));
  
  if (!id || isNaN(id)) {
    return new Response(
      JSON.stringify({ error: "Missing or invalid blog ID" }),
      { status: 400, headers: { "Content-Type": "application/json" } }
    );
  }

  // 模拟返回博客详情
  const blogDetail = createBlogDetail(id);

  return new Response(
    JSON.stringify(blogDetail),
    { status: 200, headers: { "Content-Type": "application/json" } }
  );
}
